import { v4 as uuidv4 } from 'uuid';
import { invoke } from '@tauri-apps/api/core';
import Database from "@tauri-apps/plugin-sql";
import { SqliteSettings } from '@/services';

export function uuid(): string {
    return uuidv4();
}

export function uuidTrim(): string {
    return uuidv4().replaceAll('-', '');
}

export function setLocalStorage(key: string, value: string) {
    localStorage.setItem(key, value);
}

export function getLocalStorage(key: string): string | null {
    let value = localStorage.getItem(key);
    return value;
}

export const clearLocalStorage = (key: string | undefined) => {
    if (key === undefined) {
        return localStorage.clear();
    } else {
        return localStorage.removeItem(key);
    }
};

export function getLocalStorageWithDefault(key: string, defaultValue?: string): string {
    let value = localStorage.getItem(key);
    if (value != null) {
        return value;
    }
    if (value === null && defaultValue) {
        return defaultValue
    }
    return '';
}

export function getLocalStorageObjectWithDefault<T>(key: string, defaultValue: T): T {
    let v = getLocalStorage(key);
    if (!v) {
        return defaultValue;
    }
    try {
        let obj = JSON.parse(v);
        return obj;
    } catch (err) {
        console.error(err);
    }
    return defaultValue;
}

export async function homePath(): Promise<string> {
    const path: string = await invoke('plugin:mg-platform|home_path');
    return path;
}


let SYSTEM_DATABASE = null;

export async function getG01DataBase(): Promise<Database> {
    if (SYSTEM_DATABASE == null) {
        let home = await homePath();
        SYSTEM_DATABASE = await Database.load(`sqlite:${home}/rrg01.sqlite`);
    }
    return SYSTEM_DATABASE;
}

let SQLITE_SETTINGS: SqliteSettings | null = null;

export async function getG01Settings(): Promise<SqliteSettings> {
    if (SQLITE_SETTINGS == null) {
        let home = await homePath();
        SQLITE_SETTINGS = await new SqliteSettings(`${home}/rrg01.sqlite`);
    }
    return SQLITE_SETTINGS;
}

let DEFAULT_PROVIDER = null;

export async function getDefaultProvider(): Promise<SqliteSettings | null> {
    if (DEFAULT_PROVIDER == null) {
        let settingService = await getG01Settings();

        let vstr = await settingService.getValueByCategory("ai_settings", "default_setting", '');
        if (vstr == '') {
            return null;
        }
        console.log(vstr);
        DEFAULT_PROVIDER = JSON.parse(vstr);
    }
    return DEFAULT_PROVIDER;
}

export const logout = () => {
    clearLocalStorage('g01_token');
};

export const isLogin = (): boolean => {
    let token = getLocalStorage('g01_token');
    if (token) {
        return true;
    }
    //从缓存中获取到
    return false;
};

export const getUserId = (): string | null => {
    let userId = getLocalStorage('g01_userid');
    if (userId) {
        return userId;
    }
    //从缓存中获取到
    return null;
};

export const setToken = (token: string) => {
    setLocalStorage('g01_token', token);
};

export const getToken = (): string | null => {
    let token = getLocalStorage('g01_token');
    if (token) {
        return token;
    }
    //从缓存中获取到
    return null;
};


const COLORS = [
    '#5B8FF9',
    '#F6BD16',
    '#5AD8A6',
    '#945FB9',
    '#E86452',
    '#6DC8EC',
    '#FF99C3',
    '#1E9493',
    '#FF9845',
    '#5D7092',
];
